package com.huihuiyun.huihuiyun.server.controller.agent.website;

import com.huihuiyun.huihuiyun.server.controller.agent.website.viewobjects.User;
import com.huihuiyun.huihuiyun.server.service.HhyBookService;
import com.mark.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.map.PassiveExpiringMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.HashMap;
import java.util.Map;

@Path(Const.context)
@Component
@Slf4j
public class AgentWebsiteController {
    @Autowired
    private HhyBookService bookService;

    @Context
    private HttpServletRequest httpServletRequest;

    @Context
    private HttpServletResponse httpServletResponse;

    private PassiveExpiringMap<String, String> loginUserMap = new PassiveExpiringMap(30 * 60 * 1000); // Expiration time of 1 sec

    //    {"name":"Serati Ma","avatar":"https://gw.alipayobjects.com/zos/rmsportal/BiazfanxmamNRoxxVxka.png","userid":"00000001","notifyCount":12}
    @GET
    @Path("/currentUser")
    @Produces(MediaType.APPLICATION_JSON)
    public Response currentUser() {
        try {
            String clientIPAddress = httpServletRequest.getRemoteAddr();
            if (loginUserMap.containsKey(clientIPAddress)) {
                User user = new User();
                user.name = "Serati Ma";
                user.avatar = "https://gw.alipayobjects.com/zos/rmsportal/BiazfanxmamNRoxxVxka.png";
                user.userid = "00000001";
                user.notifyCount = 12;
                return Response.ok(user).build();
            } else {
                return Response.status(Response.Status.NO_CONTENT).build();
            }
        } catch (Exception e) {
            log.error("", e);
            return Response.serverError().build();
        }
    }

    /**
     * 输入
     * <code>
     * {
     * "credentials":"include","method":"POST",
     * "body":" {\"userName\":\"admin\",\"password\":\"888888\",\"remember\":true,\"type\":\"account\"}",
     * "headers":{"Accept":"application/json","Content-Type":"application/json; charset=utf-8"}
     * }
     * </code>
     * 输出
     * <code>
     * {"status":"ok","type":"account"}
     * </code>
     */
    @POST
    @Path("/login/account")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response loginAccount(Map<String, String> data) {
        try {
            log.error("loginAccount, data=" + JsonUtil.bean2Json(data));
            String name = data.get("userName");
            String password = data.get("password");
            String type = data.get("type");
            String clientIPAddress = httpServletRequest.getRemoteAddr();
            if ("admin".equals(name) && "888888".equals(password)) {
                loginUserMap.put(clientIPAddress, clientIPAddress);
                Map map = new HashMap();
                map.put("status", "ok");
                map.put("type", type);
                map.put("currentAuthority", "admin");
                return Response.ok(map).build();
            }

            if ("user".equals(name) && "123456".equals(password)) {
                loginUserMap.put(clientIPAddress, clientIPAddress);
                Map map = new HashMap();
                map.put("status", "ok");
                map.put("type", type);
                map.put("currentAuthority", "user");
                return Response.ok(map).build();
            }
            Map map = new HashMap();
            map.put("status", "error");
            map.put("type", "account");
            map.put("currentAuthority", "guest");
            return Response.ok(map).build();
        } catch (Exception e) {
            log.error("", e);
            return Response.serverError().build();
        }
    }
}
